const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
  
    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  }
  
  const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
  }
  
  //转换时间格式 （年月日）
  function setTime(timestr){
    var date = new Date(timestr);
    var Y = date.getFullYear();
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
    var D = change(date.getDate());
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return Y+'年'+M+'月'+D+'日';
  }
  
  //转换时间格式 （时分）
  function setTime2(mss){
    var days = parseInt(mss / (1000 * 60 * 60 * 24));
    var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = (mss % (1000 * 60)) / 1000;
    return hours + " : " + minutes;
  }
  
  
  function setTime3(time){
    let timearr = time.replace(" ", ":").replace(/\:/g, "-").split("-");
    let timestr = timearr[3] + " : " + timearr[4]
    return timestr
  }
  
  //转换时间格式 （月日）
  function setTime4(timestamp) {
    var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    // var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    var D = change(date.getDate()) + ' ';
    var h = change(date.getHours()) + ':';
    var m = change(date.getMinutes()) + ':';
    var s = change(date.getSeconds());
    return  M + '.' + D;
  }
  
  function change(t) {
    if (t < 10) {
        return "0" + t;
    } else {
        return t;
    }
  }
  
  //转换时间格式 （时分秒）
  function setTime5(mss){
    var days = parseInt(mss / (1000 * 60 * 60 * 24));
    var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = parseInt((mss % (1000 * 60)) / 1000);
    return hours + ":" + minutes + ":" + seconds;
  }
  
  //转换时间格式 （月日）
  function setTime6(str){
    str = str.replace(/[^\d]/g,'/');
    str= (str.substring(str.length-1) == '/')? str.substring(0,str.length-1) : str ;
    var date = new Date(str);
    var Y = date.getFullYear();
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
    var D = change(date.getDate());
    return M +'.' + D
  }
  
  //转换时间格式 （年月日）
  function setTime7(timestr){
    var date = new Date(timestr);
    var Y = date.getFullYear();
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
    var D = change(date.getDate());
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return Y + '-' + M + '-' + D;
  }
  
  //转换时间格式 （年月日）
  function setTime8(timestr){
    var date = new Date(timestr * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear();
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    var D = change(date.getDate());
    var h = change(date.getHours()) + ':';
    var m = change(date.getMinutes()) + ':';
    var s = change(date.getSeconds());
    return Y + '/' + M + '/' + D;
  }
  
  //转换时间格式 （年月日）
  function setTime9(timestr){
    var date = new Date(timestr * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear();
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    var D = change(date.getDate());
    var h = change(date.getHours()) + ':';
    var m = change(date.getMinutes()) + ':';
    var s = change(date.getSeconds());
    return Y + '-' + M + '-' + D;
  }
  
  //转换时间格式 （年月日）
  function setTime10(timestr){
    var date = new Date(timestr);
    var Y = date.getFullYear();
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
    var D = change(date.getDate());
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return Y + '-' + M;
  }
  
  //转换时间格式 （年月日）
  function setTime11(timestr){
    var date = new Date(timestr);
    var Y = date.getFullYear();
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
    var D = change(date.getDate());
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return Y+'-'+M+'-'+D + ' ' + h +':' + minute;
  }
  
  //转换时间格式 （年月日）
  function setTime12(timestr){
    var date = new Date(timestr * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear();
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    var D = change(date.getDate());
    var h = change(date.getHours()) + ':';
    var m = change(date.getMinutes()) + ':';
    var s = change(date.getSeconds());
    return Y + '-' + M + '-' + D+ ' ' + h + ':' + m;
  }
  
  //转换时间格式 （日）
  function setTime13(str){
    str = str.replace(/[^\d]/g,'/');
    str= (str.substring(str.length-1) == '/')? str.substring(0,str.length-1) : str ;
    var date = new Date(str);
    var Y = date.getFullYear();
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
    var D = change(date.getDate())
    return D
  }
  
  //转换时间格式 （时分）
  function setTime14(timestr){
    var date = new Date(timestr);
    var Y = date.getFullYear();
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
    var D = date.getDate();
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return h + ':' + minute;
  }
  
  //转换时间格式 （月日）
  function setTime15(str){
    str = str.replace(/[^\d]/g,'/');
    str= (str.substring(str.length-1) == '/')? str.substring(0,str.length-1) : str ;
    var date = new Date(str);
    var Y = date.getFullYear();
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
    var D = change(date.getDate());
    return M + '-' + D
  }
  
  //转换时间格式 （年月日）
  function setTime16(timestr){
    var date = new Date(timestr);
    var Y = date.getFullYear();
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
    var D = change(date.getDate());
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return Y+'-'+M+'-'+D + ' ' + h + ':' + minute +':' + second;
  }
  
  //正则验证
  function validation (data,type) {
    let str = ''
    if(type == 'phone')
    {
      str = /^1\d{10}$/
    }
    else if(type == 'idcard')
    {
      str = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
    }
    else if(type == 'name')
    {
      str = /^[\u4E00-\u9FA5A-Za-z]+$/
    }
    else if(type == 'number')
    {
      // 验证非负浮点数
      str = /^(\-|\+)?\d+(\.\d+)?$/
    }
    else if(type == 'alphanumeric')
    {
      // 验证由数字和26个英文字母组成的字符串
      str = /^[A-Za-z0-9]+$/
    }
    if (str.test(data)) {
      return true
    }
    else{
      return false
    }
  }
  
  //判断是否为特殊字符或者表情
  // function judgeSpecialCharacters(nickName){
  //   reg = /[~#^$@%&!?%*]/gi;
  //   // reg.test(nickName.trim())
  //   if (isEmojiCharacter(nickName.trim())) {
  //     return true;
  //   }
  // }
  
  //判断是否是表情
  function isEmojiCharacter(substring) {
    for (var i = 0; i < substring.length; i++) {
      var hs = substring.charCodeAt(i);
      if (0xd800 <= hs && hs <= 0xdbff) {
        if (substring.length > 1) {
          var ls = substring.charCodeAt(i + 1);
          var uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
          if (0x1d000 <= uc && uc <= 0x1f77f) {
            return true;
          }
          }
      } else if (substring.length > 1) {
        var ls = substring.charCodeAt(i + 1);
        if (ls == 0x20e3) {
          return true;
        }
      } else {
        if (0x2100 <= hs && hs <= 0x27ff) {
          return true;
        } else if (0x2B05 <= hs && hs <= 0x2b07) {
          return true;
        } else if (0x2934 <= hs && hs <= 0x2935) {
          return true;
        } else if (0x3297 <= hs && hs <= 0x3299) {
          return true;
        } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
          return true;
        }
      }
    }
  }
  
  function setUnit(name){
    switch (name) {
      case 'BMI':
        return ''
      case '体脂率':
        return '%'
      case '体蛋白质':
        return 'kg'
      case '基础代谢':
        return 'kcal'
      case '体肌肉量':
        return 'kg'
      case '去脂体重':
        return 'kg'
      case '无机盐':
        return 'kg'
      case '骨矿含量':
        return 'kg'
      default:
        return 'kg'
    }
  }
  //分钟转换小时
  function ChangeHourMinutestr(str) {
    if (str !== "0" && str !== "" && str !== null) {
      return ((Math.floor(str / 60)).toString().length < 2 ? "0" + (Math.floor(str / 60)).toString() :
      (Math.floor(str / 60)).toString()) + ":" + ((str % 60).toString().length < 2 ? "0" + (str % 60).toString() : (str % 60).toString());
    }
    else
    {
      return "00:00";
    }
  }
  
  function showModal(title,content){
    wx.showModal({
      title: title,
      content: content,
      showCancel:false,
      success (res) {
      }
    })
    return
  }
  
  function showToast(title,icon,duration){
    wx.showToast({
      title: title,
      icon: icon,
      duration: 2000,
    })
  }
  
  //左选择日期
  function leftDate(date,type){
    var dateArr = date.split('-')
    if(type == 1) // 月
    {
      let Y = parseInt(dateArr[0])
      let M = parseInt(dateArr[1])
      if(M > 1){
        M = M-1;
      }
      if(M == 1){
        M = 12
        Y = Y-1;
      }
      Y = change(Y)
      M = change(M)
      return Y + '-' + M
    }else{ //日
      let Y = parseInt(dateArr[0])
      let M = parseInt(dateArr[1])
      let D = parseInt(dateArr[dateArr.length - 1])
      if(D == 1){//当日期为1时，再剪就会改变月份，甚至年份
        switch(M){
          case 1:
            D = 31;
            M = 12;
            Y = Y - 1;
            break; //一月一日 再剪一天 年份减一 月份为12 日期为31
          case 2:
          case 4:
          case 6:
          case 8:
          case 9:
          case 11:
            D = 31;
            M = M-1;
            break; //这些月一日 再剪一天 月份减一 日期为31
          case 3:
            if(Y % 4 == 0 && Y % 100 !=0 || Y % 400 ==0){
              D = 29;
              M = M -1;
            }else {
              D = 28;
              M = M -1;
            }
            break; //三月一日 再剪一天 月份减一 日期为根据是否是闰年来判断 日期
          case 5:
          case 7:
          case 10:
          case 12:
            D = 30;
            M = M -1;
            break; //这些月一日 再剪一天 月份减一 日期为30
        }
      }else{
        D = D-1;
      }
      Y = change(Y)
      M = change(M)
      D = change(D)
      return Y + '-' + M + '-' + D
    }
  }
  
  //右选择日期
  function rightDate(date,type){
    var dateArr = date.split('-')
    if(type == 1) // 月
    {
      let Y = parseInt(dateArr[0])
      let M = parseInt(dateArr[1])
      if(M == 12){
        M = 1
        Y = Y + 1
      }else{
        M = M + 1
      }
      Y = change(Y)
      M = change(M)
      return Y + '-' + M
    }else{ //日
      let Y = parseInt(dateArr[0])
      let M = parseInt(dateArr[1])
      let D = parseInt(dateArr[dateArr.length - 1])
      if(D == 28){//当日期为28号时 只判断是否是2月
        switch(M){
          case 2:
            if(Y % 4 == 0 && Y % 100 !=0 || Y%400 ==0){
              D = D +1;
            } //如果是闰年2月 日期就加一
            else {
              D = 1;
              M = M +1;
            } //不是闰年2月 日期就变为1 月份加一
            break;
          default:
            D = D +1;
            break;
        }
      }else if(D == 29){ //当日期为29号是 也是判断是否是2月
        switch(M){
          case 2: //当29号出现必定是闰年 日期变为1 月份加一
            D = 1;
            M = M +1;
            break;
          default:
            D = D +1;
            break;
        }
      }else if(D == 30){ //当日期为30 时
        switch(M){
          case 1:
          case 3:
          case 5:
          case 7:
          case 8:
          case 10:
          case 12:
            D = D +1;
            break; //这些月份的时候一个月有31天 到30的时候再加一
          case 4:
          case 6:
          case 9:
          case 11:
            D = 1;
            M = M +1;
            break; //这些月份的时候一个月有30天 到30的时候 日期变为1 月份加一
        }
      }else if(D == 31){
        switch(M){
          case 1:
          case 3:
          case 5:
          case 7:
          case 8:
          case 10:
            D = 1;
            M = M+1;
            break; //这些月份的时候一个月有31天 到31的时候 日期为1月份加一
          case 12:
            D = 1;
            M = 1;
            Y = Y+1;
            break; //十二月 的 31 号 日期变为一 月份变为一 年份加一
        }
      }else{
        D = D + 1;
      }
      Y = change(Y)
      M = change(M)
      D = change(D)
      return Y + '-' + M + '-' + D
    }
  }
  
  //wx.request
  function sendAjax(url,method,callback,data){
    wx.request({
        url:url,
        data:data,
        method:method,
        success(res){
  
            callback(res)
        }
    })
  }
  function postAjax(url,data,callback){
      wx.request({
          url:url,
          data:data,
          method:'POST',
          header:{
              'content-type':'application/x-www-form-urlencoded'
          },
          success(res){
              callback(res)
          }
      })
  }
  //排序
  function compare(property) {
    return function (a, b) {
      var value1 = a[property];
      var value2 = b[property];
      return value1 - value2;
    }
  }
  
  //弹窗
  function bounced(newWeight,oldWeight){
    var prompt = ''//提示
    var slogan = ''//标语
    var kg = ''//公斤
    var integral = '' //积分
    var crack = '' //裂纹
    if(newWeight < oldWeight){
      //瘦了
      prompt = '恭喜你',
      slogan = '瘦',
      kg = Number(oldWeight - newWeight).toFixed(1)
      integral = ''
    }else if(newWeight > oldWeight){
      prompt = '很遗憾',
      slogan = '胖',
      kg = Number(newWeight - oldWeight).toFixed(1)
      integral = '您未获得积分'
      crack = 'https://jzj.freei.cc/Uploads/ce/crack.png'
    }else if(newWeight == oldWeight){
      return false
    }
    let arr = {prompt:prompt,slogan:slogan,kg:kg,integral:integral,crack:crack}
    return arr
  }
  
  function dataZoom(list){
    var start = 0
    if(list.length > 20){
      start = 75
    }else if(list.length > 10){
      start = 45
    }
    else if(list.length > 7 && list.length <= 10){
      start = 25
    }
    return start
  }
  
  //获得积分
  function getIntegral(url,type){
    wx.request({
      url: url,
      data: {
        type: type,
      },
      method: 'get',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        console.log(res);
        return res.data.jf
      },
      fail: function (res) {
        util.showModal('提示','获取积分失败')
        return false
      }
    })
  }
  
  function kcal(weight,step){
    return kcal = parseInt(weight * ( ((step / 2 )* 1.5) / 1000) * 0.6 )
  }
  
  module.exports = {
    formatTime: formatTime,
    setTime: setTime,
    setTime2: setTime2,
    setTime3: setTime3,
    setTime4: setTime4,
    setTime5: setTime5,
    setTime6: setTime6,
    setTime7: setTime7,
    setTime8: setTime8,
    setTime9: setTime9,
    setTime10: setTime10,
    setTime11: setTime11,
    setTime12: setTime12,
    setTime13: setTime13,
    setTime14: setTime14,
    setTime15: setTime15,
    setTime16: setTime16,
    change: change,
    validation: validation,
    isEmojiCharacter: isEmojiCharacter,
    setUnit : setUnit,
    ChangeHourMinutestr: ChangeHourMinutestr,
    showModal: showModal,
    showToast: showToast,
    leftDate: leftDate,
    rightDate: rightDate,
    sendAjax:sendAjax,
    postAjax:postAjax,
    compare: compare,
    bounced: bounced,
    dataZoom: dataZoom,
    getIntegral: getIntegral,
    kcal: kcal,
  }
  